{extend name="base"/}
{block name="resources"}
<style>
    .form-wrap {margin-top: 0;}
</style>
{/block}
{block name="main"}
<div class="layui-form form-wrap">
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>海报名称：</label>
        <div class="layui-input-block">
            <input name="poster_name" type="text" lay-verify="poster_name" lay-verify="required" {notempty name="$poster_data"} value="{$poster_data.poster_name}" {/notempty} class="layui-input len-long" autocomplete="off">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>选择模板：</label>
        <div class="layui-input-block">
            <button class="layui-btn" onclick="">选择模板</button>
        </div>
        <input type="hidden" name="json_data">
    </div>

    <div class="form-row">
        <input type="hidden" name="poster_id" {notempty name="$poster_data"} value="{$poster_data.poster_id}" {/notempty}>
        <button class="layui-btn" lay-submit lay-filter="save">提交</button>
        <button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
    </div>
</div>
{/block}
{block name="script"}
<script>
    layui.use(['form', 'laydate'], function() {
        var form = layui.form,
            laydate = layui.laydate,
            repeat_flag = false; //防重复标识
        form.render();

        laydate.render({
            elem: '#laydate'
        });


        var upload = new Upload({
            elem: '#qr_img'
        });

        /**
         * 表单验证
         */
        form.verify({
            poster_name: function(value){
                if (!/[\S]+/.test(value)) {
                    return '请输入海报名称';
                }
            },
            // community_describe: function(value){
            //     if (!/[\S]+/.test(value)) {
            //         return '请输入社群描述';
            //     }
            // },
            // qr_img: function(value){
            //     if (!/[\S]+/.test(value)) {
            //         return '请上传社群二维码';
            //     }
            // }
        });

        /**
         * 监听提交
         */
        form.on('submit(save)', function(data) {

            // 删除图片
            if(!data.field.qr_img) upload.delete();

            if(repeat_flag) return false;
            repeat_flag = true;

            $.ajax({
                url: ns.url("shop/goods/editposter"),
                data: data.field,
                dataType: 'JSON', //服务器返回json格式数据
                type: 'POST', //HTTP请求类型
                success: function(res) {
                    repeat_flag = false;
                    if (res.code == 0) {
                        layer.confirm('操作成功', {
                            title:'操作提示',
                            btn: ['返回列表'],
                            closeBtn: 0,
                            yes: function(){
                                location.href = ns.url("shop/goods/poster")
                            },
                        });
                    }else{
                        layer.msg(res.message);
                    }
                }
            });
        });

    });

    function back() {
        location.href = ns.url("shop/goods/poster");
    }
</script>
{/block}